/*
 *  author: Honrun
 */
#include "stm32f4xx_hal.h"
#include "stdio.h"
#include "DevicesWatchDog.h"


static IWDG_HandleTypeDef st_typeIwdgHandle = {0};


void vWatchdogInit(void)
{
    /* Set counter reload value to obtain 250ms IWDG Timeout.
        IWDG counter clock Frequency = LsiFreq / 32
        Counter Reload Value = 250ms / IWDG counter clock period
                            = 0.25s / (32/LsiFreq)
                            = LsiFreq / (32 * 4)
                            = LsiFreq / 128 */
    st_typeIwdgHandle.Instance = IWDG;
    st_typeIwdgHandle.Init.Prescaler = IWDG_PRESCALER_32;
    st_typeIwdgHandle.Init.Reload    = 40000 / 32;

    if(HAL_IWDG_Init(&st_typeIwdgHandle) != HAL_OK)
    {
        /* Initialization Error */
        printf("HAL_IWDG_Init error.\r\n");
    }
}

void vWatchdogReload(void)
{
    /* Refresh IWDG: reload counter */
    if(HAL_IWDG_Refresh(&st_typeIwdgHandle) != HAL_OK)
    {
        /* Refresh Error */
        printf("HAL_IWDG_Refresh error.\r\n");
    }
}
